﻿using GameProtocol;
using GameProtocol.DTO;
using System;
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class AccountCtrl : SystemCtrlBase<AccountCtrl>, ISystemCtrl
{
    private UILoginView m_UILoginView;//登录窗口

    public AccountCtrl():base()
    {
        NetDispatcher.Instance.AddEventListener(LoginProtocol.LOGIN_SRES, LoginCallBack);
        NetDispatcher.Instance.AddEventListener(LoginProtocol.REG_SRES, RegistCallBack);
    }
    #region DicNotificationInterests 注册事件
    /// <summary>
    /// 注册事件
    /// </summary>
    /// <returns></returns>
    public override Dictionary<string, DispatcherBase<UIDispatcher, object[], string>.Handler> DicNotificationInterests()
    {
        Dictionary<string, UIDispatcher.Handler> dic = new Dictionary<string, DispatcherBase<UIDispatcher, object[], string>.Handler>();
        dic.Add("BtnQuickLogin", QuickLogin);
        dic.Add("BtnEnter", AccountLogin); 
        return dic;
    }
    #endregion
    public void OpenView(UIWindowType type)
    {
        switch (type)
        {
                case UIWindowType.Login:
                OpenLoginView();
                break;
        }
    }

    void OpenLoginView()
    {
        UIViewUtil.Instance.LoadWindowAsync(UIWindowType.Login, (GameObject go) =>
        {
            m_UILoginView = go.GetComponent<UILoginView>();
        });
    }
    void QuickLogin(object[] obj)
    {
        Debug.Log("走注册");
        
        NetWorkSocket.Instance.Send(
                new SocketModel(Protocol.TYPE_LOGIN, 0, LoginProtocol.REG_CREQ, new AccountInfoDTO() { PassToken = PlayerPrefs.GetString("pass_token") }
                    ), GlobalInit.Instance.SocketHandle);
    }

    void AccountLogin(object[] obj)
    {
        if (PlayerPrefs.HasKey("account_id"))
        {
            Debug.Log("走登录");
            int AccountId = PlayerPrefs.GetInt("account_id");
            string PassToken;
            if (PlayerPrefs.HasKey("pass_token"))
            {
                PassToken = PlayerPrefs.GetString("pass_token");
                Debug.Log("PassToken:" + PassToken);
                NetWorkSocket.Instance.Send(
               new SocketModel(Protocol.TYPE_LOGIN, 0, LoginProtocol.LOGIN_CREQ, new AccountInfoDTO() { AccountId = AccountId, PassToken = PassToken }
                   ), GlobalInit.Instance.SocketHandle);
            }
             
           
        }
       
    }
    void LoginCallBack(object data)
    {
        SocketModel model = data as SocketModel;
        Debug.Log("Login用户为：" + model.GetMessage<int>());
        
    }
    void RegistCallBack(object data)
    {
        SocketModel model = data as SocketModel;
        int accId = model.GetMessage<int>();
        Debug.Log("Regist用户为：" + accId);
        Debug.Log("pass_token：" + PlayerPrefs.GetString("pass_token"));
        PlayerPrefs.SetInt("account_id", accId);
    }
}
